package com.gaojiawei.multiThread05_ThreeSpecialAtomic;

import org.junit.Test;

/**
 * 使用synchronize解决保证原子性
 */
public class l02_AtomicWithSynchronize {
    private static int count;

    @Test
    public void testAtomicSynchronize() throws InterruptedException {

        Thread thread = new Thread(() -> {
            sync();
        });

        Thread thread1 = new Thread(() -> {
            sync();
        });

        thread.start();
        thread1.start();

        Thread.sleep(3000);
        System.out.println(count);

    }

    public void sync(){
        for(int i = 0;i<1000;i++){
            synchronized (l02_AtomicWithSynchronize.class){
                count++;
            }

        }
    }
}
